#!/bin/sh

TOPDIR=$1
INST_LIBS=$2
INST_INCLUDE=$3
INST_SRC=$4
INST_EXAMPLE_SRC=$5
INSTALLCMD=$6
COMPILER=$7
OPTF=$8
shift; shift; shift; shift; shift; shift; shift; shift;
DEPARG=$1
LINKARGS=$2
OSG_LIBS=$3
FREETYPE_LIB=$4
GLUT_LIB=$5
GL_LIBS=$6
X_LIBS=$7
SOCKET_LIBS=$8
shift; shift; shift; shift; shift; shift; shift; shift;
OTHER_LIBS=$1
TIFF_LIB=$2
MAKE=$3

copy_example_source()
{
    for d in `ls $TOPDIR/examples/ | grep -v CVS`
    do
        if [ -d $TOPDIR/examples/$d ]
        then
            THISDIR=`pwd`
            cd $TOPDIR/examples/$d
            [ -d $INST_EXAMPLE_SRC/$d ] || mkdir -m 0755 -p $INST_EXAMPLE_SRC/$d
            echo "         installing $d"
            for f in `$MAKE __instfiles | grep -v $MAKE`
           do
                src=`echo $f | cut -f1 -d"="`
                dst=`echo $f | cut -f2 -d"="`
                if [ -z "$dst" ] ; then dst=$src; fi
                ###echo $INSTALLCMD `pwd`/$src $INST_EXAMPLE_SRC/$d/$dst
                $INSTALLCMD `pwd`/$src $INST_EXAMPLE_SRC/$d/$dst
            done
            cd $THISDIR
        fi
    done
}

generate_makedefs()
{
    cat <<-EOF 

TOPDIR              ?= ../
THISDIR             = .
INC                 = -I$INST_INCLUDE -I./
DEF                 = $DEF
CXXFLAGS            = \$(ARCHARGS) $OPTF \$(DEF) \$(INC)
LDFLAGS             = \$(ARCHARGS) $OPTF -L$INST_LIBS $LINKARGS
OBJS                = \$(C++FILES:.cpp=.o) \$(CXXFILES:.cpp=.o) \$(CFILES:.c=.o) 
C++                 = $COMPILER
OSG_LIBS            = $OSG_LIBS 
FREETYPE_LIB        = $FREETYPE_LIB
GLUT_LIB            = $GLUT_LIB
GL_LIBS             = $GL_LIBS
X_LIBS              = $X_LIBS
SOCKET_LIBS         = $SOCKET_LIBS
OTHER_LIBS          = $OTHER_LIBS
TIFF_LIB            = $TIFF_LIB
VPATH               = ..

EOF
}



generate_makerules()
{
    cat <<-EOF1 | sed 's/TAB/	/' 

all : \$(EXEC) \$(LIB) \$(PLUGIN)

\$(EXEC) : Makedepend \$(OBJS) 
TAB\$(C++) \$(LDFLAGS) \$(OBJS) \$(LIBS) -o \$@

\$(LIB) \$(PLUGIN) : \$(OBJS)
TAB\$(C++) \$(LDFLAGS) \$(SHARED) \$(OBJS) \$(LIBS) -o \$@

clean : 
TABrm -f \$(OBJS) \$(MAKEDEPEND)

clobber : clean
TABrm -f \$(EXEC) \$(LIB) \$(PLUGIN)


Makedepend : \$(CXXFILES) \$(CFILES) 
TAB\$(C++) \$(INC) $DEPARG \$? > \$@


.SUFFIXES: .cpp .o
.cpp.o:
TAB\$(C++) \$(CXXFLAGS) -c $<

sinclude Makedepend

EOF1
}


[ -d $INST_SRC/Make ] || mkdir -m 0755 -p $INST_SRC/Make

echo =========== Installing Example Source Code =================

echo "  generating make files ..."
generate_makedefs > $INST_SRC/Make/makedefs
generate_makerules > $INST_SRC/Make/makerules

echo "  copying example files ..."
copy_example_source

exit 0
